package com.id.supaclean.ui

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.lifecycle.lifecycleScope
import com.id.supaclean.MyApplication.Companion.appContext
import com.id.supaclean.R
import com.id.supaclean.common.BaseFragment
import com.id.supaclean.dao.KeyDao
import com.id.supaclean.utils.*
import kotlinx.coroutines.launch
import java.text.DecimalFormat


class FirstFragment : BaseFragment() {

    //保留两位小数
    val df2 = DecimalFormat("0.00")

    //保留一位小数
    val df1 = DecimalFormat("0.0")

    var apkNum = 0
    var emptyFileNum = 0
    var thumbnailNum = 0

    override fun layoutId(): Int = R.layout.fragment_first

    override fun onResume() {
        super.onResume()
        view?.findViewById<TextView>(R.id.last_scan_tv)?.text = KeyDao.getCurrentLastScanTime()
    }

    override fun initView() {
        val purchaseBtn = view?.findViewById<ImageView>(R.id.sub_iv)
//        purchaseBtn?.visibility = if (!KeyDao.getCurrentPurchaseState()) View.VISIBLE else View.GONE
        view?.findViewById<View>(R.id.first_fragment_lav)?.setOnClickListener {
            KeyDao.refreshScanTime()
            navigate(R.id.action_firstFragment_to_cleanActivity)
        }
        view?.findViewById<View>(R.id.junk_iv)?.setOnClickListener {
            KeyDao.refreshScanTime()
            navigate(R.id.action_firstFragment_to_cleanActivity)
        }

        view?.findViewById<View>(R.id.ram_iv)?.setOnClickListener {
            KeyDao.refreshScanTime()
            val bundle = Bundle()
            bundle.apply {
                putString("used_storage", view?.findViewById<TextView>(R.id.used_tv)?.text.toString())
            }
            bundle.apply {
                putString("all_storage", view?.findViewById<TextView>(R.id.all_tv)?.text.toString())
            }
            bundle.apply {
                putString("used_ram", df1.format(getAvailMemory()))
            }
            navigate(R.id.action_firstFragment_to_memoryUsageFragment, bundle)
        }
        view?.findViewById<View>(R.id.sub_iv)?.setOnClickListener {
            this@FirstFragment.requireActivity()
                .startActivity(Intent(appContext, PurchaseActivity::class.java))
        }
        view?.findViewById<View>(R.id.game_iv)?.setOnClickListener {
            lifecycleScope.launch {
//                if (isSubscribe().catch { emit(false) }.first()) {

                    KeyDao.refreshScanTime()
                    val bundle = Bundle()
                    bundle.apply {
                        putString("used_storage", view?.findViewById<TextView>(R.id.used_tv)?.text.toString())
                    }
                    bundle.apply {
                        putString("all_storage", view?.findViewById<TextView>(R.id.all_tv)?.text.toString())
                    }
                    bundle.apply {
                        putString("used_ram", df1.format(getAvailMemory()))
                    }
                    navigate(R.id.action_firstFragment_to_gameUsageFragment, bundle)

            }

        }


    }

    override fun initEvent() {
        super.initEvent()
        //保留两位小数

        val allStore = "${getTotalMemory()} GB"
        val freeStore = "${df2.format(getTotalMemory() - getAvailMemory())} GB"
        val rateStore = "${getMemoryUsage().toInt()}%"
        view?.findViewById<TextView>(R.id.free_tv)?.text = freeStore
        view?.findViewById<TextView>(R.id.used_tv)?.text = rateStore
        view?.findViewById<TextView>(R.id.all_tv)?.text = allStore
    }


}